class Cup extends egret.Sprite{
	private cup:egret.Bitmap;
	private arr_stick;
	private arr_stickAngle;
	private arr_speed;
	private arr_blalpha;
	private arr_x;
	private arr_y;

	private originX;
    private bl_shake;

	private BGShape:egret.Shape;

    private whole:egret.Sprite;

	private sound:egret.Sound;
	
	public constructor() {
		super();
		this.bl_shake=false;

        this.whole=new egret.Sprite();
		this.BGShape=new egret.Shape();
		this.BGShape.graphics.beginFill(0x000000,0.4);
		this.BGShape.graphics.drawRect(0,0,700,700);
		// this.whole.addChild(this.BGShape);
		this.BGShape.x=100;this.BGShape.y=300;

		this.sound=RES.getRes("shake_mp3");
		

		this.cup=new egret.Bitmap(RES.getRes("cup_png"));
        this.cup.anchorOffsetX=this.cup.width/2;
		this.cup.anchorOffsetY=this.cup.height/2;

        this.arr_stick=new Array();
		this.arr_stickAngle=new Array();
		this.arr_speed=new Array();
		this.arr_blalpha=new Array();
		this.arr_x=new Array();
		this.arr_y=new Array();
		for(var z=0;z<6;z++){
			var stick=new egret.Bitmap(RES.getRes("stick2_png"));
			this.arr_stick.push(stick);
			stick.anchorOffsetY=stick.height;
			stick.x=740/2-30+(Math.random()-0.5)*60;
			stick.y=1136/2+10+(Math.random()-0.5)*100;

            stick.scaleX=0.7;
			stick.scaleY=0.7;

			this.arr_speed.push(0);
			this.arr_blalpha.push(false);
			this.arr_x.push(stick.x);
			this.arr_y.push(stick.y);

			stick.rotation=(Math.random()-0.5)*20;
			if(stick.rotation>0){
			this.arr_stickAngle.push(true);				
			}
			else{
			this.arr_stickAngle.push(false);				
			}
			this.whole.addChild(stick);
			stick.alpha=0;
			stick.touchEnabled=true;
		}
		this.cup.alpha=0;
		this.cup.x=740/2;
		this.cup.y=1136/2;
		this.cup.scaleX=0;
		this.cup.scaleY=0;

		this.whole.addChild(this.cup);

		this.whole.x=-500;
		this.whole.y=-600;

 		this.x=500;     
        this.y=500;		 
	

		this.addChild(this.whole);

		console.log("whole1:",this.whole.width,this.whole.height);
		
	
	    this.addEventListener(egret.Event.ENTER_FRAME,this.EF,this);
	}
    private TestShake(e:TouchEvent){
		this.Shake();
		
		
	}

	public EF(){
		if(!this.bl_shake){
		for(var z=0;z<this.arr_stick.length;z++){
			if(this.arr_stick[z].alpha==1){
				if(this.arr_stick[z].rotation>10){
					this.arr_speed[z]=0;
					this.arr_stickAngle[z]=false;
				}
				if(this.arr_stick[z].rotation<-10){
					this.arr_speed[z]=0;
					this.arr_stickAngle[z]=true;
				}
				if(this.arr_stickAngle[z]){
					this.arr_speed[z]+=Math.random()*0.1;
					this.arr_stick[z].rotation+=this.arr_speed[z];
				}
				else{
					this.arr_speed[z]+=Math.random()*0.1;
					this.arr_stick[z].rotation-=this.arr_speed[z];
				}
			}
		}
		}
	}
	public Appearance(){
		this.bl_shake=false;
		this.scaleX=1;this.scaleY=1;
		this.cup.alpha=0;
		this.y=500
		egret.Tween.get(this.cup).wait(1000).to({alpha:1,scaleX:1.1,scaleY:1.1},500,egret.Ease.backOut);
		for(var z=0;z<this.arr_stick.length;z++){
			this.arr_stick[z].y-=150+Math.random()*50;
			this.arr_stick[z].alpha=0;
			this.arr_stick[z].scaleX=0.7;
			this.arr_stick[z].scaleY=0.7;
			egret.Tween.get(this.arr_stick[z]).wait(1200+150*z).to({alpha:1,y:this.arr_y[z]},500,egret.Ease.cubicOut);
		}
		    //**********
		this.cup.touchEnabled=true;			
		// this.cup.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.TestShake,this);
		//**********
	}

	public Shake(){
		ShakeSound();
		var v=150;
		var myThis=this;
		if(!this.bl_shake){
			egret.Tween.get(this).to({x:550},v).to({x:440},v).to({x:530},v).to({x:500},v);
			this.bl_shake=true;

			var no=Math.round(Math.random()*5);
			egret.Tween.get(this.arr_stick[no]).wait(1000).to({rotation:5,x:340,y:this.arr_y[no]-40,scaleX:1.25,scaleY:1.25},1000,egret.Ease.cubicOut);
			this.arr_stick[no].addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.TB,this);
			
			setTimeout(function(){
				egret.Tween.get(myThis).to({y:600},500,egret.Ease.cubicOut);
				myThis.y=600
			},500)		
		}
	}
	private TB(e:egret.TouchEvent){
		
		egret.Tween.get(this).to({scaleX:0,scaleY:0},500,egret.Ease.cubicOut);


		for(var z=0;z<this.arr_stick.length;z++){
			this.arr_stick[z].removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.TB,this);
		}

		//*******
		this.cup.touchEnabled=false;	
		this.cup.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.TestShake,this);
		//*******


		this.dispatchEvent(new LoadEvent("GoEnd"));
		
	}
}
declare function ShakeSound()